home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Libraries / SAT / Misc / transSkel.p < prev   
Encoding:
Text File  |  1993-09-19  |  47.4 KB  |  1,594 lines  |  [TEXT/PJMM]

  1. {    TransSkel version 2.00 - Transportable application skeleton}
  2.  
  3. {    TransSkel is public domain and was originally written in LightSpeed C by:}
  4.  
  5. {            Paul DuBois}
  6. {            Wisconsin Regional Primate Research Center}
  7. {            1220 Capital Court}
  8. {            Madison WI  53706  USA}
  9.  
  10. {    UUCP:    [allegra,ihnp4,seismo]!uwvax!rhesus!dubois }
  11. {    ARPA:    dubois@rhesus.primate.wisc.edu}
  12. {    The Pascal Version of TransSkel is public domain and was ported and changed by        }
  13.  
  14. {            Owen Hartnett            }
  15. {            Ωhm Software            }
  16. {            163 Richard Drive        }
  17. {            Tiverton, RI 02878        }
  18.  
  19. {    CSNET:    omh@cs.brown.edu.CSNET                                             }
  20. {    ARPA:        omh%cs.brown.edu                                                    }
  21. {    UUCP:        [ihnp4,allegra]!brunix !omh                                            }
  22.  
  23. {    This version of TransSkel written for Lightspeed Pascal.  Lightspeed Pascal is a}
  24. {    trademark of:}
  25. {            THINK Technologies, Inc}
  26. {            420 Bedford Street  Suite 350}
  27. {            Lexington, MA  02173  USA}
  28.  
  29. {  History}
  30. {  06/13/86    Beta version. (pd) }
  31. {  08/27/86    Version number changed to 1.01.(pd)}
  32. {              v1.0 DoGrow bug fixed - the port at the point of the}
  33. {              InvalRect could have been anything; the fix is to set}
  34. {              the port to the grown window first.  This also explains}
  35. {              why the kludge to DoActivate in v1.0 worked.(pd)}
  36. {  10/02/86    Version number changed to 1.02, as a result of adding}
  37. {            modifications by David W. Berry (well!dwb@lll-lcc.arpa)}
  38. {            for supporting window zooming.  Also used his modifications}
  39. {            for supporting modeless dialogs (though not in the same}
  40. {            form).  Dialogs can be #define'd on or off.(pd)}
  41.  
  42. {12/ 28 / 86 Version number changed to 1.03 . Modified to work under LightspeedC v . 2.01 }
  43. {            - took out definitions for window zooming stuff , as it is now supported by the compiler}
  44. {             directly . Also declared DoZoom static , fixing an oversight . ( pd )}
  45. { 01 / 18 / 86 Put a SetPort into DoZoom - ZoomWindow requires port to be}
  46. {            set to window being zoomed . ( pd )}
  47. { 02 / 05 / 86 Version number changed to 1.04 . Big change : port setting behavior made explicit}
  48. {             - the only persistant switch occurs when a window comes active . This changes }
  49. {            underlying programming model ( see manual for detailed discussion ) . Thanks to}
  50. {             Duane Williams for pointing out that this should be done . Typedef 'd }
  51. {            integer/long variables to Integer, Longint to facilitate coversion to other C }
  52. {            compilers . More complete type-casting done . LightspeedC does a lot of it }
  53. {            automatically , other compilers may not . ( pd - this version never released ) }
  54. {03 / 02 / 87 Fixed bug whereby clicks in drag region of non - active windows may not bring }
  55. {            window to front . Seems to be due to DragWindow calling StillDown to see    if mouse is still }
  56. {            down .    If the machine was busy    otherwise when click occurred and }
  57. {            mouse already up when DragWindow is called , the click ends up being ignored . }
  58. {            Thanks to Roger Humphrey for finding this one . }
  59.  
  60. {* * * Changes implemented first by omh to Pascal Version}
  61.  
  62. { 12/24/86  Finished first Pascal version.   Dialogs cannot be defined off.     (omh)}
  63.  
  64. {4 / 18 / 87 Changed Desk Accessory code so it 's more tolerant of memory}
  65. {            conditions for desk accessories . ( omh ) }
  66. {7 / 12 / 87 Added "cache " code to GetWDHandler . Now TransSkel figures }
  67. {            that an event is most likely to occur for the same window as the previous }
  68. {            event . Thus the WindowPtr and WDHandle for events are cached and examined }
  69. {            to avoid searching through the handler list . ( omh ) }
  70. {7 / 12 / 87 Excised the notorious "SetPort "excess . As pointed out by Duane Williams ,}
  71. {             SetPort traps abounded unnecessarily in version 1.02 . These have been eliminated }
  72. {            now with two exceptions . First , the port is set when a window handler }
  73. {            is installed . The justification for this is that when a handler is installed , it }
  74. {            is likely that further processing will be done on it immediately . The application gets }
  75. {            control immediately after the handler is installed anyway , so this behavior can be manually }
  76. {            overridden where necessary . Second , when a window is activated , the port is}
  77. {            set to it . This follows the model of keeping the port in sync with the }
  78. {            active window . ( omh ) }
  79. {7 / 14 / 87 Added grow zone function installation    and MoreMasters to SkelInit , }
  80. {            which now requires two parameters . The first indicates the number of times to call }
  81. {            MoreMasters . The second is a ProcPtr indicating a user - supplied grow zone}
  82. {            function to be called when memory problems occur .  If nil , no grow zone}
  83. {            function is    installed . ( omh ) }
  84. {7 / 14 / 87 SkelMenu , SkelWindow , and SkelDialog now return zero or non - zero to indicate }
  85. {            failure or success of handler allocation . This could break * all * previous TransSkel }
  86. {            applications ( as will the change to SkelInit , above . Please see the section "How to }
  87. {            adapt old TransSkel to New "    in the manual    for detailed specifications on }
  88. {            how to convert your old programs . TransSkel becomes more memory conscious}
  89. {            with these changes . The functions SkelMenu , SkelWindow , and }
  90. {            SkelDialog are the only routines which actually allocate memory . Since they may be }
  91. {            called at any time , knowing that you have enough memory becomes important . Thus , }
  92. {            these routines return a value to indicate what happened .  If they return zero ,}
  93. {             then memory allocation failed . ( omh ) }
  94. {10 / 21 / 87 Added another parameter to SkelMenu:  drawBar: Boolean . This tells SkelMenu }
  95. {            whether to draw the menu bar after adding the Menu . This is done to eliminate }
  96. {            the menus popping up one at a time . Simply call SkelMenu  with drawBar false}
  97. {            until the    last time you call SkelMenu , then call it (for the last menu )}
  98. {            with drawBar true . ( omh ) }
  99. {10 / 26 / 87 Removed declarations for zoom - in and zoom - out . Added Pascal }
  100. {            changes ( above ) to C version . ( omh )}
  101.  
  102. {    02 / 02 / 88 Merged pd 's 1.04 changes with those of omh, above, to create}
  103. {            release version 2.0 . Fixed bug whereby cmd - key equivalents}
  104. {            for menu selections would execute twice if DA window in front . Thanks }
  105. {            to Don Fredkin and Julian Vrieslander for finding this one , and to Don for the}
  106. {             best fix . ( pd ) }
  107. { 10/28/88 Removed all New Rom calls. }
  108. { 10/28/88 Added support for conditional compilation for dialogs and MPW support.  By setting }
  109. {            the Think_pascal flag to false, TransSkel will run under MPW. Now correctly written for LSP 2.0}
  110.  
  111. {Some fixes done later by Ingemar Ragnemalm. Search for "Ingemar" fo find these.}
  112. {They include some bug fix and hiearcical menu support.}
  113.  
  114. {New fix 18/9-93: Added a filter proc for dialogs, to allow special event processing before}
  115. {an event is passed to IsDialogEvent, when a modeless dialog is in the front. This makes it possible}
  116. {to use user items in modeless dialogs, and to handle return/enter.}
  117.  
  118. unit TransSkel;
  119.  
  120. interface
  121.  
  122. {$SETC supportDialogs:= true }
  123.                     { Set to false to disallow modeless dialog support and save code space }
  124.                 { Set to false to have SkelInit call QuickDraw Inits: InitGraf, InitDialog, etc. }
  125.  
  126. {$IFC UNDEFINED THINK_PASCAL}
  127.  
  128.     uses
  129.         Memtypes, Quickdraw, OSIntf, ToolIntf, PackIntf;
  130. {$ENDC}
  131.  
  132.     procedure SkelInit (NoMasters: integer; myGrowZone: ProcPtr);
  133.     procedure SkelMain;
  134.     procedure SkelWhoa;
  135.     procedure SkelClobber;
  136.     function SkelMenu (theMenu: MenuHandle; pSelect: ProcPtr; pClobber: ProcPtr; DrawBar: Boolean): Boolean;
  137.     function SkelHMenu (theMenu: MenuHandle; pSelect: ProcPtr; pClobber: ProcPtr): Boolean; {Added by Ingemar 22/8 -93}
  138.     procedure SkelRmveMenu (theMenu: MenuHandle);
  139.     procedure SkelApple (aboutTitle: Str255; aboutProc: ProcPtr);
  140.     function SkelWindow (theWind: WindowPtr; pMouse, pKey, pUpdate, pActivate, pClose, pClobber, pIdle: ProcPtr; frontOnly: Boolean): Boolean;
  141.     procedure SkelRmveWind (theWind: WindowPtr);
  142. {$IFC supportDialogs }
  143.     function SkelDialog (theDialog: DialogPtr; pEvent, pClose, pClobber, pFilter: ProcPtr): Boolean; {pFilter dded by Ingemar 18/9-93}
  144.     procedure SkelRmveDlog (theDialog: DialogPtr);
  145. {$ENDC}
  146.     procedure SkelGrowBounds (theWind: WindowPtr; hLO, vLo, hHi, vHi: integer);
  147.     procedure SkelEventMask (mask: integer);
  148.     procedure SkelGetEventMask (var mask: integer);
  149.     procedure SkelBackground (p: ProcPtr);
  150.     procedure SkelGetBackground (var p: ProcPtr);
  151.     procedure SkelEventHook (p: ProcPtr);
  152.     procedure SkelGetEventHook (var p: ProcPtr);
  153. {$IFC supportDialogs }
  154.     procedure SkelDlogMask (mask: integer);
  155.     procedure SkelGetDlogMask (var mask: integer);
  156. {$ENDC}
  157.  
  158.  
  159. implementation
  160.  
  161.     const
  162.         mBarHeight = 20;    { menu bar height.  All window sizing}
  163.  
  164.         GrowZoneSize = 4000;        {  Size of memory to be freed when GrowZone Proc called }
  165.  
  166. {    This window zooming stuff may need to be removed if you use the new Rom libraries        }
  167. {    if not, then you can add zooming without the overhead of the new Rom libs.  See TrackBox    }
  168. {     routine also.                                                                                            }
  169.  
  170. {    Window and Menu handler types, constants, variables.}
  171.  
  172. {    whList and mhList are the lists of window and menu handlers.}
  173. {    whClobOnRmve and mhClobOnRmve are true if the handler disposal proc}
  174. {    is to be called when a handler is removed.  They are temporarily set}
  175. {    false when handlers are installed for windows or menus that already}
  176. {    have handlers - the old handler is removed WITHOUT calling the}
  177. {    disposal proc.}
  178.  
  179. {    Default lower limits on window sizing of 80 pixels both directions is}
  180. {    sufficient to allow text windows room to draw a grow box and scroll}
  181. {    bars without having the thumb and arrows overlap.  These values may}
  182. {    be changed if such a constraint is undesirable with SkelGrowBounds.}
  183. {    Default upper limits are for the Macintosh, not the Lisa, but are set}
  184. {    per machine in SkelInit.}
  185.  
  186.     type
  187.         WHandlerPtr = ^WHandler;
  188.         WHandlerHnd = ^WHandlerPtr;
  189.         WHandler = record
  190.                 whWind: WindowPtr;    {window/dialog to be handled    }
  191.                 whClobber: ProcPtr;    { data structure disposal proc    }
  192.                 whMouse: ProcPtr;        { mouse-click handler proc        }
  193.                 whKey: ProcPtr;        { key-click handler proc            }
  194.                 whUpdate: ProcPtr;        { update handler proc                }
  195.                 whActivate: ProcPtr;    { activate event handler proc    }
  196.                 whClose: ProcPtr;        { close "event" handler proc        }
  197.                 whIdle: ProcPtr;            { main loop proc                    }
  198. {$IFC supportDialogs }
  199.                 whEvent: ProcPtr;        { dialog event proc                }
  200.                 whFilter: ProcPtr;        { dialog filter proc ADDED BY INGEMAR 18/9 -93}
  201. {$ENDC }
  202.                 whHasGrow: Boolean;    { can window grow?                }
  203.                 whGrow: Rect;            { limits on window sizing        }
  204.                 whSized: Boolean;        { true = window was resized    }
  205.                 whFrontOnly: Boolean;    { true = idle only when active    }
  206.                 whNext: WHandlerHnd;            { next window handler            }
  207.             end;
  208.  
  209.         MHandlerPtr = ^MHandler;
  210.         MHandlerHnd = ^MHandlerPtr;
  211.  
  212.         MHandler = record
  213.                 mhID: integer;                { menu id                                    }
  214.                 mhSelect: ProcPtr;            { item selection handler proc            }
  215.                 mhClobber: ProcPtr;        { menu disposal handler proc            }
  216.                 mhNext: MHandlerHnd;        { next menu handler                        }
  217.             end;
  218.  
  219.     var
  220.         whList: WHandlerHnd;                { list of menu handlers }
  221.         whClobOnRmve: Boolean;
  222.         growRect: Rect;
  223.         mhList: MHandlerHnd;
  224.         mhClobOnRmve: Boolean;
  225.  
  226. {    Variables for default Apple menu handler.  appleID is set to 1 if}
  227. {    SkelApple is called and is the id of the Apple menu, appleAboutProc}
  228. {    is the procedure to execute if there is an About... item and it's}
  229. {    chosen from the Apple menu.  If doAbout is true, then the menu}
  230. {    contains the About... item, otherwise it's just desk accessories.}
  231.  
  232.         appleMenu: MenuHandle;
  233.         appleID: integer;
  234.         appleAboutProc: ProcPtr;
  235.         doAbout: Boolean;
  236.  
  237. {    Miscellaneous}
  238.  
  239. {    screenPort points to the window manager port.}
  240.  
  241. {    doneFlag determines when SkelMain returns.  It is set by calling}
  242. {    SkelWhoa(), which the host does to request a halt.}
  243.  
  244. {    pBkgnd points to a background procedure, to be run during event}
  245. {    processing.  Set it with SkelBackground.  If nil, there's no}
  246. {    procedure.}
  247.  
  248. {    pEvent points to an event-inspecting hook, to be run whenever an}
  249. {    event occurs.  Set it with SkelEventHook.  If nil, there's no}
  250. {    procedure.}
  251.  
  252. {    eventMask controls the event types requested in the GetNextEvent}
  253. {    call in SkelMain.}
  254.  
  255. {    diskInitPt is the location at which the disk initialization dialog}
  256. {    appears, if an uninitialized disk is inserted.}
  257.  
  258.         screenPort: GrafPtr;
  259.         doneFlag: integer;
  260.         pBkgnd: ProcPtr;
  261.         pEvent: ProcPtr;
  262.         eventMask: integer;
  263.         diskInitPt: Point;
  264.  
  265. {$IFC supportDialogs }
  266.  
  267. {    Events that are passed to dialogs.  Others are ignored.}
  268. {    Standard mask passes , mousedown, keydown, autokey, update,}
  269. {    activate and null events.  Null events are controlled by bit 0.}
  270.  
  271.         dlogEventMask: integer;
  272. {$ENDC}
  273.         pEventflag: Boolean;
  274.  
  275. {    "caching" global variables.  previous version would search down the window }
  276. {    list for every event it found.  Now, if the event happened to the same window }
  277. {    as last time, GetWDHandler will just do a simple compare }
  278. {    and return the last window handler.  This speeds up multiple window applications }
  279. {    immensely, at only a slight cost when you activate a new window (one }
  280. {    compare!)  If you don't like it, use the old version. }
  281.  
  282.         oldWindow: WindowPtr;
  283.         oldWDHandler: WHandlerHnd;
  284.  
  285. {    Global for built in "Grow Zone" function  }
  286.  
  287.         safetyHandle: Handle;
  288.  
  289.         myDitl: packed array[0..100] of byte;
  290.  
  291. { Rather than including the entire new ROM libraries, with all the other stuff you might not use    }
  292. { I've instead included just the Zoom box stuff here.  Depending on your status, you can either    }
  293. { leave things as they are, and only use zooming from the new Rom libs, or comment out the        }
  294. { calls, and include the new Rom libraries if you want to incorporate other new Rom calls        }
  295.  
  296.  
  297. { -------------------------------------------------------------------- }
  298. {                        Internal (private) Routines                                    }
  299. { -------------------------------------------------------------------- }
  300.  
  301.  
  302.  
  303. {    Get handler associated with user or dialog window.}
  304. {    Return nil if window doesn't belong to any known handler.}
  305. {    This routine is absolutely fundamental to TransSkel.}
  306.  
  307.     function GetWDHandler (theWind: WindowPtr): WHandlerHnd;
  308.  
  309.         var
  310.             h: WHandlerHnd;
  311.     begin
  312.         h := WhList;
  313.         GetWDHandler := nil;
  314.         if theWind = oldWindow then            {  caching code     }
  315.             GetWDHandler := oldWDHandler
  316.         else
  317.             while h <> nil do
  318.                 if h^^.whWind = theWind then
  319.                     begin
  320.                         oldWindow := theWind;            { Load in new values for new window }
  321.                         oldWDHandler := h;
  322.                         GetWDHandler := h;
  323.                         h := nil;
  324.                     end
  325.                 else
  326.                     h := WHandlerHnd(h^^.whNext);
  327.     end;
  328.  
  329. { Get Handler associated with user window.  Return nil if window doesn't}
  330. {  have a Handler. }
  331.  
  332.     function GetWHandler (theWind: WindowPtr): WHandlerHnd;
  333.  
  334.         var
  335.             h: WHandlerHnd;
  336.             myPeek: WindowPeek;
  337.  
  338.     begin
  339. {BUG FIXED by Ingemar 19/9-93. This function retured garbage when passed a dialog}
  340.         h := GetWDHandler(theWind);
  341.         myPeek := WindowPeek(theWind);
  342.         GetWHandler := nil; {default, moved up by Ingemar}
  343.         if h <> nil then
  344.             begin
  345.                 if mypeek^.windowKind <> dialogKind then
  346.                     GetWHandler := h;
  347.             end;
  348.     end;
  349.  
  350. {$IFC supportDialogs }
  351.  
  352. {    Get handler associated with dialog window.}
  353. {    Return nil if window doesn't belong to any known handler.}
  354.  
  355.     function GetDHandler (theDialog: WindowPtr): WHandlerHnd;
  356.  
  357.         var
  358.             h: WHandlerHnd;
  359.             myPeek: WindowPeek;
  360.  
  361.     begin
  362. {BUG FIXED by Ingemar 19/9-93. This function retured garbage when passed a non-dialog}
  363.         h := GetWDHandler(theDialog);
  364.         myPeek := WindowPeek(theDialog);
  365.         GetDHandler := nil; {default - moved up by Ingemar}
  366.         if h <> nil then
  367.             begin
  368.                 if mypeek^.windowKind = dialogKind then
  369.                     GetDHandler := h;
  370.             end;
  371.     end;
  372. {$ENDC}
  373.  
  374. {The following procedures are Pascal "glue" that allows Pascal to call a Procedure    }
  375. { from a ProcPtr.  It is similar to (*p) () construct used in the C dialect.  Different    }
  376. { procedures are necessary for the reason of Pascal's strongly typed parameter        }
  377. { list.  Fortunately, there are not too many calls which use different param lists        }
  378.  
  379.     procedure callpMouse (thePoint: Point; theTime: longint; theMods: integer; myProc: ProcPtr);
  380.  
  381.     inline
  382.         $205f,     {movea.l  (a7)+,a0        ; (a0) is a ptr to string, 4(a0) is mode}
  383.         $4e90;
  384.  
  385.     procedure callpKey (theChar: char; theMods: integer; myProc: ProcPtr);
  386.  
  387.     inline
  388.         $205f,     {movea.l  (a7)+,a0        ; (a0) is a ptr to string, 4(a0) is mode}
  389.         $4e90;
  390.  
  391.     procedure callpEvent (theitem: integer; var theEvent: EventRecord; myProc: ProcPtr);
  392.  
  393.     inline
  394.         $205f,     {movea.l  (a7)+,a0        ; (a0) is a ptr to string, 4(a0) is mode}
  395.         $4e90;
  396.  
  397.     function callotherEvent (var theEvent: EventRecord; myProc: ProcPtr): Boolean;
  398.  
  399.     inline
  400.         $205f, $4e90;
  401.  
  402.  
  403.     procedure callpBoolean (myBool: Boolean; myProc: ProcPtr);
  404.  
  405. { Two calls use Booleans as one parameter arguments.  This procedure handles    }
  406. { both of them.                                                                            }
  407.  
  408.     inline
  409.         $205f,     {movea.l  (a7)+,a0        ; (a0) is a ptr to string, 4(a0) is mode}
  410.         $4e90;
  411.  
  412.     procedure callpInt (myInt: integer; myProc: ProcPtr);
  413.  
  414. { Two calls use Booleans as one parameter arguments.  This procedure handles    }
  415. { both of them.                                                                            }
  416.  
  417.     inline
  418.         $205f,     {movea.l  (a7)+,a0        ; (a0) is a ptr to string, 4(a0) is mode}
  419.         $4e90;
  420.  
  421.     procedure callpMenu (myMenu: MenuHandle; myProc: ProcPtr);
  422.  
  423. { Handle removeal of menus.     }
  424.  
  425.     inline
  426.         $205f,     {movea.l  (a7)+,a0        ; (a0) is a ptr to string, 4(a0) is mode}
  427.         $4e90;
  428.  
  429.     procedure callpnoarg (myProc: ProcPtr);
  430.  
  431. { For all the Procedures that are called with no arguments                            }
  432.  
  433.     inline
  434.         $205f,     {movea.l  (a7)+,a0        ; (a0) is a ptr to string, 4(a0) is mode}
  435.         $4e90;
  436.  
  437.  
  438. {ADDED BY INGEMAR 18/9-93 to support new dialog filters}
  439. {IS IT POSSIBLE to use this for functions?}
  440.     procedure callpfilter (theDialog: DialogPtr; var theEvent: EventRecord; var result: Boolean; myProc: ProcPtr);
  441.     inline
  442.         $205f,     {movea.l  (a7)+,a0        ; (a0) is a ptr to string, 4(a0) is mode}
  443.         $4e90;
  444.  
  445. {    General menu-handler.  Just passes selection to the handler's}
  446. {    select routine.  If the select routine is nil, selecting items from}
  447. {    the menu is a nop.}
  448.  
  449.     procedure DoMenuCommand (command: longint);
  450.  
  451.         var
  452.             menu: integer;
  453.             item: integer;
  454.             mh: MHandlerHnd;
  455.             p: ProcPtr;
  456.  
  457.     begin
  458.         menu := HiWord(command);
  459.         item := LoWord(command);
  460.         mh := mhList;
  461.         while (mh <> nil) do
  462.             begin
  463.                 p := mh^^.mhSelect;
  464.                 if ((menu = mh^^.mhID) and (p <> nil)) then
  465.                     begin
  466.                         callpInt(item, p);
  467.                         mh := nil;
  468.                     end
  469.                 else
  470.                     mh := mh^^.mhNext;
  471.             end;
  472.         HiliteMenu(0);
  473.     end;
  474.  
  475. {    Apple menu handler}
  476.  
  477. {    DoAppleItem:  If the first item was chosen, and there's an "About..."}
  478. {    item, call the procedure associated with it (if not nil).  If there}
  479. {    is no "About..." item or the item was not the first one, then open}
  480. {    the associated desk accessory.  The port is saved and restored}
  481. {    because OpenDeskAcc does not always preserve it correctly.}
  482.  
  483. {    DoAppleClobber disposes of the Apple menu.}
  484.  
  485.     procedure DoAppleItem (item: integer);
  486.  
  487.         var
  488.             curPort: GrafPtr;
  489.             str: Str255;
  490.             ignore: integer;
  491.             h: Handle;
  492.  
  493.     begin
  494.         if doAbout and (item = 1) then
  495.             begin
  496.                 if appleAboutProc <> nil then
  497.                     callpnoarg(appleAboutProc);
  498.             end
  499.         else
  500.             begin
  501.                 GetPort(curPort);
  502.                 GetItem(appleMenu, item, str);
  503.                 SetResLoad(false);
  504.                 h := GetNamedResource('DRVR', str);
  505.                 SetResLoad(true);
  506.                 if h <> nil then
  507.                     begin
  508.                         ResrvMem(SizeResource(h) + $1000);
  509.                         ignore := OpenDeskAcc(str);
  510.                     end;
  511.                 SetPort(curPort);
  512.             end;
  513.     end;
  514.  
  515.     procedure DoAppleClobber;
  516.     begin
  517.         DisposeMenu(appleMenu);
  518.     end;
  519.  
  520. { --------------------------------------------------------------------     }
  521. {                        Window-handler routing routines                            }
  522. {                                                                                        }
  523. {    Each routine sets the port to the handler's window before executing        }
  524. {    the handler procedure.                                                            }
  525. { --------------------------------------------------------------------     }
  526.  
  527.  
  528. {    Pass local mouse coordinates, click time, and the modifiers flag}
  529. {    word to the handler.  Should not be necessary to set the port, as}
  530. {       the click is passed to the active window's hander. }
  531.  
  532.     procedure DoMouse (h: WHandlerHnd; theEvent: EventRecord);
  533.  
  534.         var
  535.             p: ProcPtr;
  536.             thePt: Point;
  537.  
  538.     begin
  539.         if (h <> nil) then
  540.             begin
  541.                 p := h^^.whMouse;
  542.                 if p <> nil then
  543.                     begin
  544.                         thePt := theEvent.where;
  545.                         GlobalToLocal(thePt);
  546.                         callpMouse(thePt, theEvent.when, theEvent.modifiers, p);
  547.                     end;
  548.             end;
  549.     end;
  550.  
  551. {    Pass the character and the modifiers flag word to the handler.}
  552. {    Should not be necessary to set the port, as the  click is passed to the}
  553. {    active window's handler. }
  554.  
  555.     procedure DoKey (h: WHandlerHnd; ch: char; mods: integer);
  556.         var
  557.             p: ProcPtr;
  558.  
  559.     begin
  560.         if h <> nil then
  561.             begin
  562.                 p := h^^.whKey;
  563.                 if p <> nil then
  564.                     callpKey(ch, mods, p);
  565.             end;
  566.     end;
  567.  
  568. {    Call the window updating procedure, passing to it an indicator whether the}
  569. {    window has been resized or not.  Then clear the flag, assuming the update}
  570. {    proc took whatever action was necessary to respond to resizing.}
  571. {}
  572. {    If the handler doesn't have any update proc, the Begin/EndUpdate stuff}
  573. {    is still done, to clear the update region.  Otherwise the Window Manager }
  574. {    will keep generating update events for the window, stalling updates of}
  575. {    other windows.    }
  576.  
  577. {    Make sure to save and restore the port, as it's not always the active window}
  578. {    that's updated.    }
  579.  
  580.     procedure DoUpdate (h: WHandlerHnd);
  581.  
  582.         var
  583.             rh: WhandlerHnd;
  584.             p: ProcPtr;
  585.             updPort, tmpPort: GrafPtr;
  586.  
  587.     begin
  588.         rh := h;
  589.         if rh <> nil then
  590.             begin
  591.                 GetPort(tmpPort);
  592.                 updPort := rh^^.whWind;
  593.                 SetPort(updPort);
  594.                 BeginUpdate(updPort);
  595.                 p := rh^^.whUpdate;
  596.                 if p <> nil then
  597.                     begin
  598.                         callpBoolean(rh^^.whSized, p);
  599.                         rh^^.whSized := false;
  600.                     end;
  601.                 EndUpdate(updPort);
  602.                 SetPort(tmpPort);
  603.             end;
  604.     end;
  605.  
  606. {    Pass activate/deactivate notification to handler.  On activate, set the port to}
  607. {    the window coming active    }
  608.  
  609.     procedure DoActivate (h: WHandlerHnd; active: Boolean);
  610.  
  611.         var
  612.             p: ProcPtr;
  613.  
  614.     begin
  615.         if h <> nil then
  616.             begin
  617.                 if active then
  618.                     SetPort(h^^.whWind);
  619.                 p := h^^.whActivate;
  620.                 if p <> nil then
  621.                     callpBoolean(active, p);
  622.             end
  623.     end;
  624.  
  625. {    Execute a window handler's close proc.  The close box for handlers}
  626. {    for temp windows that want to remove themselves when the window}
  627. {    is closed can call SkelRmveWind to dispose of the window}
  628. {    and remove the handler from the window handler list.  Thus, windows}
  629. {    may be dynamically created and destroyed without filling up the}
  630. {    handler list with a bunch of invalid handlers.}
  631.  
  632. {    If the handler doesn't have a close proc, just hide the window.}
  633. {    The host should provide some way of reopening the window (perhaps}
  634. {    a menu selection).  Otherwise the window will be lost from user}
  635. {    control if it is hidden, since it won't receive user-initiated events.}
  636.  
  637. {    Since the close box of only the active window may be clicked, it}
  638. {    is not necessary to set the port . }
  639.  
  640. {    This is called both for regular and dialog windows.}
  641.  
  642.     procedure DoClose (h: WHandlerHnd);
  643.  
  644.         var
  645.             rh: WHandlerHnd;
  646.             p: ProcPtr;
  647.     begin
  648.         rh := h;
  649.         if rh <> nil then
  650.             begin
  651.                 p := rh^^.whClose;
  652.                 if (p <> nil) then
  653.                     callpnoarg(p)
  654.                 else
  655.                     HideWindow(rh^^.whWind);
  656.             end;
  657.     end;
  658.  
  659. {    Execute a window Handler's clobber proc.  This is called both for regular and dialog windows.}
  660. {    Must save, set and restore port, since any window (not just active one) may be clobbered    }
  661. {    at any time.}
  662. {}
  663. {    Don't need to check whether handler is nil, as in other handler procedures, since this is only}
  664. {    called by SkelRmveWind with a known valid handler.     }
  665.  
  666.     procedure DoClobber (h: WHandlerHnd);
  667.  
  668.         var
  669.             p: ProcPtr;
  670.             curPort: Grafptr;
  671.     begin
  672.         if (h <> nil) then
  673.             begin
  674.                 GetPort(curPort);
  675.                 SetPort(h^^.whWind);
  676.                 p := h^^.whClobber;
  677.                 if p <> nil then
  678.                     callpnoarg(p);
  679.                 SetPort(curPort);
  680.             end;
  681.     end;
  682.  
  683. {$IFC supportDialogs }
  684.  
  685. {    Handle event if it's for a dialog.  The event must be one of}
  686. {    those that is passed to dialogs according to dlogEventMask.}
  687. {    This mask can be set so that disk-inserts, for instance, don't}
  688. {    get eaten up.}
  689.  
  690.     function DoDialog (theEvent: EventRecord): Boolean;
  691.  
  692.         var
  693.             dh: WHandlerHnd;
  694.             theDialog: DialogPtr;
  695.             myDPeek: DialogPeek;
  696.             what: integer;
  697.             item: integer;
  698.             tmpPort: GrafPtr;
  699.             ignore: Boolean;
  700.             testme: longint;
  701.             hasfilter, filtered: Boolean;
  702.  
  703.     begin
  704.  
  705. {    handle command keys before they get to IsDialogEvent}
  706.  
  707.         what := theEvent.what;
  708.         testme := BitShift(longint(1), what);
  709.         testme := BitAnd(testme, longint(dlogEventMask));
  710.         if (((what = keydown) or (what = autokey)) and (BitAnd(theEvent.modifiers, cmdkey) <> 0)) then
  711. { Bugfix by Ingemar above! }
  712.             begin
  713.                 DoMenuCommand(MenuKey(Char(BitAnd(theEvent.message, charCodeMask))));
  714.                 DoDialog := true;
  715.             end
  716. {Filter procedure, Added by Ingemar 18/9 -93:}
  717.         else
  718.             begin
  719. {Kolla om FrontWindow har whFilter!}
  720.                 dh := WHandlerHnd(GetDHandler(FrontWindow));
  721.                 filtered := false;
  722.                 hasfilter := dh <> nil;
  723.                 if hasfilter then
  724.                     hasfilter := dh^^.whFilter <> nil;
  725.                 if hasfilter then
  726.                     callpfilter(FrontWindow, theEvent, filtered, dh^^.whFilter);
  727.                 DoDialog := filtered;
  728.                 if not filtered then
  729. {end of filter proc handling}
  730. {else}
  731.                     if testme > 0 then
  732.                         if IsDialogEvent(theEvent) then
  733.                             begin
  734.                                 if DialogSelect(theEvent, theDialog, item) then
  735.                                     begin
  736.                                         dh := WHandlerHnd(GetDHandler(theDialog));
  737.                                         if (dh <> nil) then
  738.                                             if (dh^^.whEvent <> nil) then
  739.                                                 begin
  740.                                                     GetPort(tmpPort);
  741.                                                     SetPort(theDialog);
  742.                                                     callpEvent(item, theEvent, dh^^.whEvent);
  743.                                                     SetPort(tmpPort);
  744.                                                 end;
  745.                                     end;
  746.                                 DoDialog := true;
  747.                             end
  748.                         else
  749.                             DoDialog := false;
  750.             end; {to match begin added with filter above}
  751.     end;
  752. {$ENDC}
  753.  
  754. { -------------------------------------------------------------------- }
  755. {                            Event-handling routines                        }
  756. { -------------------------------------------------------------------- }
  757.  
  758. {    Have either sized or zoomed the window.  Invalidate it to force}
  759. {    an update and set the 'resized' flag in the window handler true.}
  760. {    The port is assumed to be set to the port that changed size.        }
  761.  
  762.     procedure TriggerUpdate (h: WHandlerHnd; thePort: GrafPtr);
  763.  
  764.     begin
  765.         InvalRect(thePort^.portRect);
  766.         if (h <> nil) then
  767.             begin
  768.                 h^^.whSized := true;
  769.             end;
  770.     end;
  771.  
  772. {    Size a window.  If the window has a handler, use the grow limits}
  773. {    in the handler record, otherwise use the defaults.}
  774.  
  775. {    The portRect is invalidated to force an update event.    The handler's}
  776. {    update procedure should check the parameter passed to it to check}
  777. {    whether the window has changed size, if it needs to adjust itself to}
  778. {    the new size.  THIS IS A CONVENTION.  Update procs must notice grow}
  779. {    "events", there is no procedure specifically for such events.}
  780.  
  781. {    The clipping rectangle is not reset.  If the host application}
  782. {    keeps the clipping set equal to the portRect or something similar,}
  783. {    then it will have to arrange to treat window growing with more}
  784. {    care.}
  785. {}
  786. {    Since the grow region of only the active window may be clicked, it should}
  787. {    not be necessary to set the port.}
  788.  
  789.     procedure DoGrow (h: WHandlerHnd; thePort: GrafPtr; StartPt: Point);
  790.  
  791.         var
  792.             r: Rect;
  793.             growRes: longint;
  794.  
  795.     begin
  796.         if (h <> nil) then
  797.             begin
  798.                 r := h^^.whGrow;
  799.             end
  800.         else
  801.             r := growRect;
  802.         growRes := GrowWindow(thePort, startPt, r);
  803.         if growRes <> 0 then
  804.             begin
  805.                 SizeWindow(thePort, LoWord(growRes), HiWord(growRes), false);
  806.                 TriggerUpdate(h, thePort);
  807.             end;
  808.     end;
  809.  
  810.  
  811. {    Zoom the current window.  Very similar to DoGrow}
  812. {    Since the zoombox of only the active window may be clicked, it should not be necessary}
  813. {    to set the port.    }
  814.  
  815.     procedure DoZoom (h: WHandlerHnd; thePort: GrafPtr; partcode: integer);
  816.  
  817.     begin
  818.         ZoomWindow(thePort, partcode, false);
  819.         TriggerUpdate(h, thePort);
  820.     end;
  821.  
  822. {    General event handler}
  823.  
  824.     procedure DoEvent (theEvt: Eventrecord);
  825.  
  826.         var
  827.             theEvent: EventRecord;
  828.             evtPt: Point;
  829.             evtPort: GrafPtr;
  830.             evtPart: integer;
  831.             evtChar: char;
  832.             evtMods: integer;
  833.             h: WHandlerHnd;
  834.             r: Rect;
  835.             ignore: integer;
  836.  
  837.     begin
  838.         theEvent := theEvt;
  839. {$IFC supportDialogs }
  840.         if not (DoDialog(theEvent)) then
  841. {$ENDC}
  842.             begin
  843.                 evtPt := theEvent.where;
  844.                 case theEvent.what of
  845.                     nullEvent: 
  846.                         ;
  847.  
  848. {    Mouse click.  Get the window that the click occurred in, and the}
  849. {    part of the window.    Get WDHandler is called here, not GetWHandler, since}
  850. {    we need the handler for a window which might turn out to be a dialog window,}
  851. {    e.g., if the click is in a close box.}
  852.  
  853.                     mouseDown: 
  854.                         begin
  855.                             evtPart := FindWindow(evtPt, evtPort);
  856.                             h := GetWDHandler(evtPort);
  857.                             case evtPart of
  858.  
  859. {    Click in a desk accessory window.  Pass back to the system.}
  860.  
  861.                                 inSysWindow: 
  862.                                     SystemClick(theEvent, evtPort);
  863.  
  864. {    Click in menu bar.  Track the mouse and execute selected command,}
  865. {    if any.}
  866.  
  867.                                 inMenuBar: 
  868.                                     DoMenuCommand(MenuSelect(evtPt));
  869.  
  870. {    Click in grow box.  Resize window.}
  871.  
  872.                                 inGrow: 
  873.                                     DoGrow(h, evtPort, evtPt);
  874.  
  875. {    Click in title bar.  Drag the window around.  Leave at least}
  876. {    4 pixels visible in both directions.    Bug fix: The window, if not front, is}
  877. {    selected first to make sure it's at least activated (unless the command key is down - see Inside}
  878. {    Macintosh).  DragWindow seems to call StillDown first, so that clicks in drag regions while}
  879. {    machine is busy don't otherwise bring window to front if the mouse is already up by the time}
  880. {    DragWindow is called.}
  881.  
  882.                                 inDrag: 
  883.                                     begin
  884.                                         if (evtPort <> FrontWindow) and (BitAnd(evtmods, cmdKey) = 0) then
  885.                                             SelectWindow(evtPort);
  886.                                         r := screenPort^.portRect;
  887.                                         r.top := r.top + mBarHeight;        { Skip down past menu bar    }
  888.                                         InsetRect(r, 4, 4);
  889.                                         DragWindow(evtPort, evtPt, r);
  890.                                     end;
  891.  
  892. {    Click in close box.  Call the close proc if the window has one.}
  893.  
  894.                                 inGoAway: 
  895.                                     if (TrackGoAway(evtPort, evtPt)) then
  896.                                         DoClose(GetWDHandler(evtPort));
  897.  
  898. {    Click in content region.  If the window wasn't frontmost (active),}
  899. {    just select it, otherwise pass the click to the window's mouse}
  900. {    click handler.}
  901.  
  902.                                 inContent: 
  903.                                     if (evtPort <> FrontWindow) then
  904.                                         SelectWindow(evtPort)
  905.                                     else
  906.                                         DoMouse(h, theEvent);
  907.  
  908. {    Click in zoom box.  Track the click and then zoom the window if}
  909. {    necessary}
  910.  
  911.                                 inZoomin, inZoomOut: 
  912.                                     if (TrackBox(evtPort, evtPt, evtPart)) then
  913.                                         DoZoom(h, evtport, evtPart);
  914.                                 otherwise
  915.                                     ;
  916.                             end;{mousedown}
  917.                         end;
  918.  
  919. {    Key event.  If the command key was down, process as menu item}
  920. {    selection, otherwise pass the character and the modifiers flags}
  921. {    to the active window's key handler.}
  922.  
  923. {    If dialogs are supported, there's no check for command-key}
  924. {    equivalents, since that would have been checked in DoDialog.}
  925.  
  926.                     keydown, autokey: 
  927.                         begin
  928.                             evtChar := char(BitAnd(theEvent.message, charCodeMask));
  929.                             evtMods := theEvent.modifiers;
  930.                             if BitAnd(evtMods, cmdKey) > 0 then
  931.                                 DoMenuCommand(menuKey(evtChar))
  932.                             else
  933.                                 DoKey(GetWHandler(FrontWindow), evtChar, evtMods);
  934.                         end;
  935.  
  936. {    Update a window.}
  937.  
  938.                     updateEvt: 
  939.                         DoUpdate(GetWHandler(WindowPtr(theEvent.message)));
  940.  
  941. {    Activate or deactivate a window.}
  942.  
  943.                     activateEvt: 
  944.                         DoActivate(GetWHandler(WindowPtr(theEvent.message)), (BitAnd(theEvent.modifiers, activeFlag) <> 0));
  945.  
  946. {    handle inserts of uninitialized disks}
  947.  
  948.                     diskEvt: 
  949.                         if (HiWord(theEvent.message) <> noErr) then
  950.                             begin
  951.                                 DILoad;
  952.                                 ignore := DIBadMount(diskInitPt, theEvent.message);
  953.                                 DIUnload;
  954.                             end;
  955.                     otherwise
  956.                 end;
  957.             end;
  958.     end;
  959.  
  960. { -------------------------------------------------------------------- }
  961. {                        Interface (public) Routines                        }
  962. { -------------------------------------------------------------------- }
  963.  
  964.  
  965. {    Initialize the various Macintosh Managers.}
  966. {    Set default upper limits on window sizing.}
  967. {    FlushEvents does NOT toss disk insert events, so that disks}
  968. {    inserted while the application is starting up don't result}
  969. {    in dead drives.}
  970. {    NoMasters is the number of times to call MoreMasters.  gzProc is the address of a user - provided}
  971. {    grow zone function procedure to call if memory gets tight.  Pass nil if none to be used.    }
  972.  
  973.     procedure SkelInit;
  974.         var
  975.             i: integer;
  976.  
  977.     begin
  978.  
  979. { For non-Lightspeed Pascal users, the following inits are included as a compile time option, }
  980. {  See the $SETC definition at the beginning of the unit.  }
  981.  
  982. {$IFC UNDEFINED THINK_PASCAL }
  983.  
  984.         InitGraf(@thePort);
  985.         InitFonts;
  986.         InitWindows;
  987.         InitMenus;
  988.         TEInit;
  989.         InitDialogs(nil);
  990.         MaxApplZone;
  991. {$ENDC}
  992.  
  993.         FlushEvents(everyEvent - diskMask, 0);
  994.         for i := 1 to NoMasters do
  995.             MoreMasters;
  996.         if myGrowZone <> nil then
  997.             SetGrowZone(myGrowZone);
  998.  
  999.         InitCursor;
  1000.         whList := nil;
  1001.         whClobOnRmve := true;
  1002.         SetRect(growRect, 80, 80, 512, 342 - mBarHeight);
  1003.         mhList := nil;
  1004.         mhClobOnRmve := true;
  1005.         appleID := 0;
  1006.         appleAboutProc := nil;
  1007.         doAbout := false;
  1008.         doneflag := 0;
  1009.         pBkgnd := nil;
  1010.         pEvent := nil;
  1011.         pEventflag := false;
  1012.         eventmask := everyEvent;
  1013.         diskInitPt.v := 120;
  1014.         diskInitPt.h := 100;
  1015. {$IFC supportDialogs }
  1016.         dlogEventMask := $16f;
  1017. {$ENDC}
  1018.  
  1019. {    Set upper limits of window sizing to machine screen size.  Allow}
  1020. {    for the menu bar.}
  1021.  
  1022.         GetWMgrPort(screenport);
  1023.         growRect.right := screenPort^.portRect.right;
  1024.         growRect.bottom := screenPort^.portRect.bottom - mBarHeight;
  1025.  
  1026. {    Set caching global variables to nil }
  1027.  
  1028.         oldWindow := nil;
  1029.         oldWDHandler := nil;
  1030.     end;
  1031.  
  1032. {    Main loop.}
  1033.  
  1034. {    Task care of DA's with SystemTask.}
  1035. {    Run background task if there is one.}
  1036. {    If there is an event, check for an event hook.  If there isn't}
  1037. {    one defined, or if there is but it returns false, call the}
  1038. {    general event handler.  (Hook returns true if TransSkel should}
  1039. {    ignore the event.)}
  1040. {    If no event, call the "no-event" handler for the front window and for}
  1041. {    any other windows with idle procedures that are always supposed}
  1042. {    to run.  This is done in such a way that it is safe for idle procs}
  1043. {    to remove the handler for their own window if they want (unlikely,}
  1044. {    but...)  This loop doesn't check whether the window is really}
  1045. {    a dialog window or not, but it doesn't have to, because such}
  1046. {    things always have a nil idle proc.}
  1047. {    }
  1048. {    doneFlag is reset upon exit.  This allows it to be called}
  1049. {    repeatedly, or recursively.}
  1050.  
  1051. {    Null events are looked at (in SkelMain)}
  1052. {    and passed to the event handler.  This is necessary to make sure}
  1053. {    DialogSelect gets called repeatedly, or the caret won't blink if}
  1054. {    a dialog has any editText items.  Null events are not passed to any event-inspecting hook that may}
  1055. {    be installed.}
  1056.  
  1057.     procedure SkelMain;
  1058.  
  1059.         var
  1060.             theEvent: EventRecord;
  1061.             wh, wh2: WHandlerHnd;
  1062.             w: WindowPtr;
  1063.             haveEvent, testpevent, testbool: Boolean;
  1064.             tmpPort: GrafPtr;
  1065.             p: ProcPtr;
  1066.  
  1067.     begin
  1068.         while (doneFlag = 0) do
  1069.             begin
  1070.                 SystemTask;
  1071.                 if (pBkgnd <> nil) then
  1072.                     callpnoarg(pBkgnd);
  1073.                 haveEvent := GetNextEvent(eventMask, theEvent);
  1074.                 if (pEvent <> nil) then
  1075.                     testpevent := CallotherEvent(theEvent, pEvent)
  1076.                 else
  1077.                     testpevent := false;
  1078. { following line fixed from version 1.02 & 1.03     [but was still buggy! /Ingemar]}
  1079. {$IFC supportDialogs }
  1080.                 if (pEvent = nil) or (testpevent = false) then {haveEvent and <- This is wrong for modeless dialogs according to page 416? Changed by Ingemar 6/8 -93}
  1081. {$ELSEC}
  1082.                     if haveEvent and ((pEvent = nil) or (testpevent = false)) then { Old line. IFC'ad by Ingemar 6/8 -93}
  1083. {$ENDC}
  1084.                         DoEvent(theEvent);
  1085.                 if not haveEvent then
  1086.                     begin
  1087.                         wh := whList;
  1088.                         GetPort(tmpPort);
  1089.                         while (wh <> nil) do
  1090.                             begin
  1091.                                 wh2 := wh^^.whNext;
  1092.                                 w := wh^^.whWind;
  1093.                                 if ((w = FrontWindow) or not wh^^.whFrontOnly) then
  1094.                                     begin
  1095.                                         SystemTask;
  1096.                                         if (wh^^.whIdle <> nil) then
  1097.                                             begin
  1098.                                                 SetPort(wh^^.whWind);
  1099.                                                 p := wh^^.whIdle;
  1100.                                                 if (p <> nil) then
  1101.                                                     callpnoarg(p);
  1102.                                             end;
  1103.                                     end;
  1104.                                 wh := wh2;
  1105.                             end;
  1106.                         SetPort(tmpPort);
  1107.                     end;
  1108.             end;
  1109.         doneFlag := 0;
  1110.     end;
  1111.  
  1112. {    Tell SkelMain to stop}
  1113.  
  1114.     procedure SkelWhoa;
  1115.     begin
  1116.         doneFlag := 1;
  1117.     end;
  1118.  
  1119. {    Clobber all the menu, window and dialog handlers}
  1120.  
  1121.     procedure SkelClobber;
  1122.  
  1123.  
  1124.     begin
  1125.         oldWDHandler := nil;
  1126.         oldWindow := nil;
  1127.         while (whList <> nil) do
  1128.             begin
  1129.                 SkelRmveWind(whList^^.whWind);
  1130.             end;
  1131.         while (mhList <> nil) do
  1132.             begin
  1133.                 SkelRmveMenu(GetMHandle(mhList^^.mhID));
  1134.             end;
  1135.     end;
  1136.  
  1137. { -------------------------------------------------------------------- }
  1138. {                        Menu-handler interface routines                            }
  1139. { -------------------------------------------------------------------- }
  1140.  
  1141.  
  1142.  
  1143.  
  1144. {    Install handler for a menu.  Remove any previous handler for it.}
  1145. {    Pass the following parameters:}
  1146.  
  1147. {    theMenu    Handle to the menu to be handled.  Must be created by host.}
  1148. {    pSelect    Proc that handles selection of items from menu.  If this is}
  1149. {            nil, the menu is installed, but nothing happens when items}
  1150. {            are selected from it.}
  1151. {    pClobber Proc for disposal of handler's data structures.  Usually}
  1152. {            nil for menus that remain in menu bar until program}
  1153. {            termination.}
  1154.  
  1155. {    The menu is installed and drawn in the menu bar.}
  1156.  
  1157. {     Return false if no handler could be allocated, true if successful. }
  1158.  
  1159.     function CommonSkelMenu (theMenu: MenuHandle; pSelect: ProcPtr; pClobber: ProcPtr): Boolean;
  1160.         var
  1161.             mh: MHandlerHnd;
  1162.             myHand: Handle;
  1163.     begin
  1164.         mhClobOnRmve := false;
  1165.         SkelRmveMenu(theMenu);
  1166.         mhClobOnRmve := true;
  1167.         myHand := NewHandle(Sizeof(MHandler));
  1168.         CommonSkelMenu := false;
  1169.         if myHand <> nil then
  1170.             begin
  1171.                 CommonSkelMenu := true;                    { show we really got the memory }
  1172.                 mh := MHandlerHnd(myHand);
  1173.                 mh^^.mhNext := mhList;
  1174.                 mhList := MHandlerHnd(myHand);
  1175.                 mh^^.mhID := theMenu^^.menuID;    { get menu id number }
  1176.                 mh^^.mhSelect := pSelect;            { install selection handler }
  1177.                 mh^^.mhClobber := pClobber;        { install disposal handler }
  1178.             end;
  1179.     end;
  1180.  
  1181. {  Install handler for a normal menu }
  1182.  
  1183.     function SkelMenu;
  1184.         var
  1185.             success: Boolean;
  1186.     begin
  1187.         success := CommonSkelMenu(theMenu, pSelect, pClobber);
  1188.         SkelMenu := success;
  1189.         if success then
  1190.             begin
  1191.                 InsertMenu(theMenu, 0);            { put menu at end of menu bar }
  1192.                 if DrawBar then
  1193.                     DrawMenuBar;
  1194.             end;
  1195.     end;
  1196.  
  1197. {    Install handler for a hiearcical menu.  Almost same as above.}
  1198. {    Added by Ingemar 22/8 -93}
  1199.  
  1200.     function SkelHMenu;
  1201.         var
  1202.             success: Boolean;
  1203.     begin
  1204.         success := CommonSkelMenu(theMenu, pSelect, pClobber);
  1205.         SkelHMenu := success;
  1206.         if success then
  1207.             begin
  1208.                 InsertMenu(theMenu, -1);            { put menu at end of menu bar }
  1209.             end;
  1210.     end;
  1211.  
  1212.  
  1213. {    Remove a menu handler.  This calls the handler's disposal routine}
  1214. {    and then takes the handler out of the handler list and disposes}
  1215. {    of it.}
  1216.  
  1217. {    Note that the menu MUST be deleted from the menu bar before calling}
  1218. {    the clobber proc, because the menu bar will end up filled with}
  1219. {    garbage if the menu was allocated with NewMenu (see discussion of}
  1220. {    DisposeMenu in Menu Manager section of Inside Macintosh).}
  1221.  
  1222.     procedure SkelRmveMenu;
  1223.  
  1224.         var
  1225.             mID: integer;
  1226.             h, h2: MHandlerHnd;
  1227.             p: ProcPtr;
  1228.             returnflag: Boolean;
  1229.  
  1230.     begin
  1231.         mID := theMenu^^.menuID;
  1232.         returnflag := false;
  1233.         if mhlist <> nil then
  1234.             begin
  1235.                 if mhList^^.mhID = mID then
  1236.                     begin
  1237.                         h2 := mhlist;
  1238.                         mhList := h2^^.mhNext;
  1239.                     end
  1240.                 else
  1241.                     begin
  1242.                         h := mhList;
  1243.                         while (h <> nil) and not returnflag do
  1244.                             begin
  1245.                                 h2 := h^^.mhNext;
  1246.                                 if (h2 = nil) then
  1247.                                     begin
  1248.                                         h := nil;
  1249.                                         returnflag := true;
  1250.                                     end
  1251.                                 else if h2^^.mhID = mID then
  1252.                                     begin
  1253.                                         h^^.mhNext := h2^^.mhNext;
  1254.                                         h := nil;
  1255.                                     end;
  1256.                                 if h <> nil then
  1257.                                     h := h2;
  1258.                             end;
  1259.                     end;
  1260.                 if not returnflag then
  1261.                     begin
  1262.                         DeleteMenu(mID);
  1263.                         DrawMenuBar;
  1264.                         p := h2^^.mhClobber;
  1265.                         if mhClobOnRmve and (p <> nil) then
  1266.                             callpMenu(theMenu, p);
  1267.                         DisposHandle(Handle(h2));
  1268.                     end;
  1269.             end;
  1270.     end;
  1271.  
  1272. {    Install a handler for the Apple menu.}
  1273.  
  1274. {    SkelApple is called if TransSkel is supposed to handle the apple}
  1275. {    menu itself.  The title is the title of the first item.  If nil,}
  1276. {    then only desk accessories are put into the menu.  If not nil, then}
  1277. {    the title is entered as the first item, followed by a gray line,}
  1278. {    then the desk accessories.}
  1279.  
  1280. {     SkelApple does not cause the menubar to be drawn, so if the Apple menu is the only menu, }
  1281. {    DrawMenuBar must be called afterward.}
  1282.  
  1283. {    No value is returned, unlike SkelMenu.  It is assumed that SkelApple will be called so early in the}
  1284. {    application that the call to SkelMenu is virtually certain to succeed.  }
  1285.  
  1286.     procedure SkelApple;
  1287.  
  1288.         var
  1289.             appleTitle: Str255;
  1290.             dummy: boolean;
  1291.     begin
  1292.         appleTitle := ' ';
  1293.         appleTitle[1] := char($14);
  1294.         appleID := 1;
  1295.         AppleMenu := NewMenu(appleID, appleTitle);
  1296.         if aboutTitle <> '' then
  1297.             begin
  1298.                 doAbout := true;
  1299.                 AppendMenu(appleMenu, aboutTitle);
  1300.                 AppendMenu(appleMenu, '(-');
  1301.                 AppleAboutProc := aboutProc;
  1302.             end;
  1303.         AddResMenu(appleMenu, 'DRVR');
  1304.         dummy := SkelMenu(appleMenu, @DoAppleItem, @DoAppleClobber, false);
  1305.     end;
  1306.  
  1307. { -------------------------------------------------------------------- }
  1308. {                    Window-handler interface routines                    }
  1309. { -------------------------------------------------------------------- }
  1310.  
  1311.  
  1312.  
  1313. {    Install handler for a window.  Remove any previous handler for it.}
  1314. {    Pass the following parameters:}
  1315.  
  1316. {    theWind    Pointer to the window to be handled.  Must be created by host.}
  1317. {    pMouse    Proc to handle mouse clicks in window.  The proc will be}
  1318. {            passed the point (in local coordinates), the time of the}
  1319. {            click, and the modifier flags word.}
  1320. {    pKey    Proc to handle key clicks in window.  The proc will be passed}
  1321. {            the character and the modifier flags word.}
  1322. {    pUpdate    Proc for updating window.  TransSkel brackets calls to update}
  1323. {            procs with calls to BeginUpdate and EndUpdate, so the visRgn}
  1324. {            is set up correctly.  A flag is passed indicating whether the}
  1325. {            window was resized or not.  BY CONVENTION, the entire portRect}
  1326. {            is invalidated when the window is resized.  That way, the}
  1327. {            handler's update proc can redraw the entire content region}
  1328. {            without interference from BeginUpdate/EndUpdate.  The flag}
  1329. {            is set to false after the update proc is called; the}
  1330. {            assumption is made that it will notice the resizing and}
  1331. {            respond appropriately.}
  1332. {    pActivate Proc to execute when window is activated or deactivated.}
  1333. {            A boolean is passed to it which is true if the window is}
  1334. {            coming active, false if it's going inactive.}
  1335. {    pClose    Proc to execute when mouse clicked in close box.  Useful}
  1336. {            mainly to temp window handlers that want to know when to}
  1337. {            self-destruct (with SkelRmveWind).}
  1338. {    pClobber Proc for disposal of handler's data structures}
  1339. {    pIdle    Proc to execute when no events are pending.}
  1340. {    frontOnly True if pIdle should execute on no events only when}
  1341. {            theWind is frontmost, false if executes all the time.  Note}
  1342. {            that if it always goes, everything else may be slowed down!}
  1343.  
  1344. {    If a particular procedure is not needed (e.g., key events are}
  1345. {    not processed by a handler), pass nil in place of the appropriate}
  1346. {    procedure address.}
  1347.  
  1348. {    Return false  if no handler could be allocated, true if successful.}
  1349.  
  1350.     function SkelWindow;
  1351.  
  1352.         var
  1353.             hHand: WhandlerHnd;
  1354.  
  1355.     begin
  1356.         whClobOnRmve := false;
  1357.         SkelRmveWind(theWind);
  1358.         whClobOnRmve := true;
  1359.  
  1360. {    Get new handler, attach to list of handlers.  It is attached to the beginning of the list, which is simpler;}
  1361. {    the order should be irrelevant to the hose, anyway. }
  1362.  
  1363.         hHand := WHandlerHnd(NewHandle(Sizeof(WHandler)));
  1364.         SkelWindow := false;
  1365.         if hHand <> nil then
  1366.             begin
  1367.                 hHand^^.whNext := whList;
  1368.                 whList := hHand;
  1369.                 with hHand^^ do
  1370.                     begin
  1371.                         SkelWindow := true;                    { Show that we got the memory }
  1372.                         whWind := theWind;
  1373.                         whMouse := pMouse;
  1374.                         whKey := pKey;
  1375.                         whUpdate := pUpdate;
  1376.                         whActivate := pActivate;
  1377.                         whClose := pClose;
  1378.                         whClobber := pClobber;
  1379.                         whIdle := pIdle;
  1380.                         whFrontOnly := frontOnly;
  1381.                         whSized := false;
  1382.                         whGrow := GrowRect;
  1383.                     end;
  1384.             end;
  1385.         SetPort(theWind);
  1386.     end;
  1387.  
  1388. {    Remove a window handler.  This calls the handler's disposal routine}
  1389. {    and then takes the handler out of the handler list and disposes}
  1390. {    of it.}
  1391.  
  1392. {    SkelRmveWind is also called by SkelRmveDlog.}
  1393.  
  1394. {    Note that if the window cache variable is set to the window whose handler is being clobbered, the }
  1395. {    variable must be zeroed.    }
  1396.  
  1397.     procedure SkelRmveWind;
  1398.  
  1399.         var
  1400.             h, h2: WHandlerHnd;
  1401.             returnflag: Boolean;
  1402.  
  1403.     begin
  1404.         if theWind = oldWindow then
  1405.             begin
  1406.                 oldWindow := nil;
  1407. {•    oldWDHandler := nil;•}
  1408.             end;
  1409.  
  1410.         if (whList <> nil) then
  1411.             begin
  1412.                 returnflag := false;
  1413.                 if whList^^.whWind = theWind then
  1414.                     begin
  1415.                         h2 := whlist;
  1416.                         whList := whList^^.whNext;
  1417.                     end
  1418.                 else
  1419.                     begin
  1420.                         h := whList;
  1421.                         while (h <> nil) and not returnflag do
  1422.                             begin
  1423.                                 h2 := h^^.whNext;
  1424.                                 if (h2 = nil) then
  1425.                                     begin
  1426.                                         h := nil;
  1427.                                         returnflag := true;
  1428.                                     end
  1429.                                 else if h2^^.whWind = theWind then
  1430.                                     begin
  1431.                                         h^^.whNext := h2^^.whNext;
  1432.                                         h := nil;
  1433.                                     end;
  1434.                                 if h <> nil then
  1435.                                     h := h2;
  1436.                             end;
  1437.                     end;
  1438.                 if not returnflag then
  1439.                     begin
  1440.                         if (whClobOnRmve) then
  1441.                             DoClobber(h2);
  1442.                         DisposHandle(Handle(h2));
  1443.                     end;
  1444.             end;
  1445.     end;
  1446.  
  1447. {$IFC supportDialogs }
  1448.  
  1449. { -------------------------------------------------------------------- }
  1450. {                    Dialog-handler interface routines                                }
  1451. { -------------------------------------------------------------------- }
  1452.  
  1453.  
  1454.  
  1455. {    Install a dialog handler.  Remove any previous handler for it.}
  1456. {    SkelDialog calls SkelWindow as a subsidiary to install a window}
  1457. {    handler, then sets the event procedure on return.}
  1458.  
  1459. {    Pass the following parameters:}
  1460.  
  1461. {    theDialog    Pointer to the dialog to be handled.  Must be created}
  1462. {            by host.}
  1463. {    pEvent    Event-handling proc for dialog events.}
  1464. {    pClose    Proc to execute when mouse clicked in close box.  Useful}
  1465. {            mainly to dialog handlers that want to know when to}
  1466. {            self-destruct (with SkelRmveDlog).}
  1467. {    pClobber Proc for disposal of handler's data structures}
  1468.  
  1469. {    If a particular procedure is not needed, pass nil in place of}
  1470. {    the appropriate procedure address.}
  1471.  
  1472. {    Return false if no handler could be allocated, true if successful.}
  1473.  
  1474.     function SkelDialog;
  1475.  
  1476.         var
  1477.             wh: WHandlerHnd;
  1478.             aBool: Boolean;
  1479.  
  1480.     begin
  1481.         aBool := SkelWindow(theDialog, nil, nil, nil, nil, pClose, pClobber, nil, false);
  1482.         if aBool <> false then
  1483.             begin
  1484.                 wh := GetWDHandler(theDialog);
  1485.                 wh^^.whEvent := pEvent;
  1486. {Added by Ingemar 18/9 -93:}
  1487.                 wh^^.whFilter := pFilter; {Install a filter function to be called *before* IsDialogEvent!}
  1488.             end;
  1489.         SkelDialog := aBool;
  1490.     end;
  1491.  
  1492. {    Remove a dialog and its handler}
  1493.  
  1494.     procedure SkelRmveDlog;
  1495.  
  1496.     begin
  1497.         SkelRmveWind(theDialog);
  1498.     end;
  1499. {$ENDC}
  1500. { -------------------------------------------------------------------- }
  1501. {                    Miscellaneous interface routines                    }
  1502. { -------------------------------------------------------------------- }
  1503.  
  1504.  
  1505. {    Override the default sizing limits for a window, or, if theWind}
  1506. {    is nil, reset the default limits used by SkelWindow.}
  1507.  
  1508.     procedure SkelGrowBounds;
  1509.  
  1510.         var
  1511.             h: WHandlerHnd;
  1512.             r: Rect;
  1513.  
  1514.     begin
  1515.         if theWind = nil then
  1516.             SetRect(growRect, hLo, vLo, hHi, vHi)
  1517.         else
  1518.             begin
  1519.                 h := GetWHandler(theWind);
  1520.                 if h <> nil then
  1521.                     begin
  1522.                         SetRect(r, hLo, vLo, hHi, vHi);
  1523.                         h^^.whGrow := r;
  1524.                     end;
  1525.             end;
  1526.     end;
  1527.  
  1528. {    Set the event mask.}
  1529.  
  1530.     procedure SkelEventMask;
  1531.  
  1532.     begin
  1533.         eventMask := mask;
  1534.     end;
  1535.  
  1536. {    Return the event mask.}
  1537.  
  1538.     procedure SkelGetEventMask;
  1539.  
  1540.     begin
  1541.         mask := eventMask;
  1542.     end;
  1543.  
  1544. {    Install a background task.  If p is nil, the current task is}
  1545. {    disabled.}
  1546.  
  1547.     procedure SkelBackground;
  1548.  
  1549.     begin
  1550.         pBkgnd := p;
  1551.     end;
  1552.  
  1553. {    Return the current background task.  Return nil if none.}
  1554.  
  1555.     procedure SkelGetBackground;
  1556.     begin
  1557.         p := pBkgnd;
  1558.     end;
  1559.  
  1560. {    Install an event-inspecting hook.  If p is nil, the hook is}
  1561. {    disabled.}
  1562.  
  1563.     procedure SkelEventHook;
  1564.  
  1565.     begin
  1566.         pEvent := p;
  1567.     end;
  1568.  
  1569.     procedure SkelGetEventHook;
  1570.  
  1571.     begin
  1572.         p := pEvent;
  1573.     end;
  1574. {$IFC supportDialogs }
  1575.  
  1576. {    Set the mask for event types that will be passed to dialogs.}
  1577. {    Bit 1 is always set, so that null events will be passed.}
  1578. {    If this is not done, the caret does not blink in editText items.}
  1579.  
  1580.     procedure SkelDlogMask;
  1581.  
  1582.     begin
  1583.         dlogEventMask := BitOr(mask, 1);
  1584.     end;
  1585.  
  1586. {    Return the current dialog event mask.}
  1587.  
  1588.     procedure SkelGetDlogMask;
  1589.  
  1590.     begin
  1591.         mask := dlogEventMask;
  1592.     end;
  1593. {$ENDC}
  1594. end.